#! @BASH@
# -*- mode: sh -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Written by Victor Ananjevsky, 2010
#

VERSION=@VERSION@
GETTEXT_DOMAIN=@PACKAGE@

file=$1
if [[ -z $file || $file == "-h" || $file == "--help" ]]; then
    echo "Usage: ${0##*/} <filename>"
    exit 1
fi
if [[ $file == "-v" || $file == "--version"  ]]; then
    echo $"${0##*/}, version - $VERSION"
    exit 0
fi

eval $(mtag --utf --shell "$file")

vals=$(@YAD@ --dialog-sep \
    --title=$"Media Tag Editor" \
    --button=gtk-save:0 --button=gtk-close:1 \
    --image=applications-multimedia \
    --text=$"Tags for file: '$file'" --form \
    --field=$"Artist:" --field=$"Title:" --field=$"Album:" --field=$"Year:" \
    --field=$"Comments:" --field=$"Genre:" --field=$"Track:" \
    "$MTAG_ARTIST" "$MTAG_TITLE" "$MTAG_ALBUM"  "$MTAG_YEAR" \
    "$MTAG_COMMENT" "$MTAG_GENRE" "$MTAG_TRACK")
ret=$?

if [[ $ret -eq 0 ]]; then
    eval tags=($(echo "$vals" | sed 's/|/\n/g' | sed 's/.*/"&"/'))
    mtag -s -u -a "${tags[0]}" -t "${tags[1]}" -A "${tags[2]}" -y "${tags[3]:-0}" -c "${tags[4]}" -g "${tags[5]}" -T "${tags[6]:-0}" "$file"
    ret=$?
fi

exit $ret
